package com.example.myapp.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapp.R;
import com.example.myapp.adapter.MyRecyclerViewAdapter;
import com.example.myapp.adapter.MyRecyclerViewNetDataAdapter;
import com.example.myapp.entity.ItemBean;
import com.example.myapp.entity.SolarTermBean;
import com.example.myapp.utils.Constants;
import com.example.myapp.utils.Tools;

import java.util.ArrayList;
import java.util.List;

public class HomeFragment extends Fragment {
    // 二十四节气即：立春、 雨水、惊蛰、春分、清明、谷雨、立夏、小满、芒种、夏至、小暑、大暑、立秋、处暑、白露、秋分、寒露、霜降、立冬、小雪、大雪、冬至、小寒、大寒。
    private String[] solarTermTexts={"立春","雨水","惊蛰","春分","清明","谷雨","立夏","小满","芒种","夏至","小暑","大暑",
            "立秋","处暑","白露","秋分","寒露","霜降","立冬","小雪","大雪","冬至","小寒","大寒"};
    private Integer[] solarTermImages = {R.mipmap.lichun,R.mipmap.yushui,R.mipmap.jingzhe,R.mipmap.chunfen,R.mipmap.qingming,R.mipmap.guyu,
            R.mipmap.lixia,R.mipmap.xiaoman,R.mipmap.mangzhong,R.mipmap.xiazhi,R.mipmap.dongzhi,R.mipmap.xiaoshu,R.mipmap.dashu,
            R.mipmap.liqiu,R.mipmap.chushu,R.mipmap.bailu,R.mipmap.qiufen,R.mipmap.hanlu,R.mipmap.jiangshuang,
            R.mipmap.lidong,R.mipmap.xiaoxue,R.mipmap.dashu,R.mipmap.dongzhi,R.mipmap.xiaohan,R.mipmap.dahan};





    private RecyclerView mRecyclerView;
    private Context context;


    public void setContext(Context  context) {
        this.context=context;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //布局文件转换view，决定这个Fragment的页面显示
        View myFragmentView = inflater.inflate(R.layout.home_fragment_layout, container, false);
        return  myFragmentView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
       //初始化控件
        //RecyclerView列表控件
        mRecyclerView = view.findViewById(R.id.mRecyclerView);
        //首页搜索布局
         LinearLayout home_search_ll = view.findViewById(R.id.home_search_ll);
         //点击搜索布局
        home_search_ll.setOnClickListener(v->{
            Tools.toastTip(context,"搜索功能还在开发中，敬请期待~");
        });


        //*创建布局管理器
        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        //设置为垂直布局，这也是默认的
        layoutManager.setOrientation(RecyclerView. VERTICAL);
        //水平
        //  layoutManager.setOrientation(RecyclerView.HORIZONTAL);
        //网格布局
        //  GridLayoutManager layoutManager2 = new GridLayoutManager(this,3);
        //*设置布局管理器
        mRecyclerView.setLayoutManager(layoutManager);

        //两种加载方式，只能选其一种
            //第一种方式：显示本地图片数据
            //localImageRes();
            //第二种方式：显示图片网络数据
            netImageRes();


    }

    //本地资源图片
    private void localImageRes(){
        //准备数据
        List<ItemBean> list = new ArrayList<>();
        for (int i = 1; i <=24; i++) {
            ItemBean itemBean = new ItemBean();
            // itemBean.setImageInt(R.mipmap.new_icon);
            itemBean.setImageInt(solarTermImages[i-1]); //设置本地图片
            //itemBean.setTitle("标题"+i);
            itemBean.setTitle(solarTermTexts[i-1]);
            itemBean.setContent("内容===="+i);
            list.add(itemBean);
        }
                //*创建适配器
      MyRecyclerViewAdapter  myAdapter = new MyRecyclerViewAdapter(context);
        //*给适配器传数据作为列表数据
        myAdapter.setList(list);
        //*设置RecyclerView的适配器
        mRecyclerView.setAdapter(myAdapter);
        //分割线
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(context, RecyclerView.VERTICAL);
        //给列表设置分割线
        mRecyclerView.addItemDecoration(dividerItemDecoration);
    }

    //网络图片数据
    private void netImageRes() {
        //准备数据
        List<SolarTermBean> solarTermBeanList = new ArrayList<>();
        for (int i = 0; i < 24; i++) {
            SolarTermBean solarTermBean = new SolarTermBean();
            solarTermBean.setTitle(Constants.solarTermTitle[i]);
            solarTermBean.setContent(Constants.solarTermContents[i]);
            solarTermBean.setImageUrl(Constants.solarTermUrlImages[i]);
            solarTermBeanList.add(solarTermBean);
        }

        //*创建适配器
        MyRecyclerViewNetDataAdapter myNetDataAdapter = new MyRecyclerViewNetDataAdapter(context);
        //*给适配器传数据作为列表数据
        myNetDataAdapter.setList(solarTermBeanList);
        //*设置RecyclerView的适配器
        mRecyclerView.setAdapter(myNetDataAdapter);
        //分割线
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(context, RecyclerView.VERTICAL);
        //给列表设置分割线
        mRecyclerView.addItemDecoration(dividerItemDecoration);
    }


}
